package cn.zwz.customer.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.basics.utils.SecurityUtil;
import cn.zwz.customer.entity.Customer;
import cn.zwz.customer.entity.Supplier;
import cn.zwz.customer.service.ICustomerService;
import cn.zwz.data.entity.User;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.customer.entity.Order;
import cn.zwz.customer.service.IOrderService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 熊英杰
 */
@Slf4j
@RestController
@Api(tags = "订单信息管理接口")
@RequestMapping("/zwz/order")
@Transactional
public class OrderController {

    @Autowired
    private IOrderService iOrderService;

    @Autowired
    private ICustomerService iCustomerService;

    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条订单信息")
    public Result<Order> get(@RequestParam String id){
        return new ResultUtil<Order>().setData(iOrderService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部订单信息个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iOrderService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部订单信息")
    public Result<List<Order>> getAll(){
        return new ResultUtil<List<Order>>().setData(iOrderService.list());
    }

    /**
     * 查询订单信息
     * @param order 客户
     * @param page 分页参数
     * @return Result<IPage<Order>>
     */
    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询订单信息")
    public Result<IPage<Order>> getByPage(@ModelAttribute Order order ,@ModelAttribute PageVo page){
        QueryWrapper<Order> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(order.getDate())) {
            qw.eq("date",order.getDate());
        }
        if(!ZwzNullUtils.isNull(order.getStatus())) {
            qw.eq("status",order.getStatus());
        }
        if(!ZwzNullUtils.isNull(order.getCustomerName())) {
            qw.eq("customer_name",order.getCustomerName());
        }
        IPage<Order> data = iOrderService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<Order>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改订单信息")
    public Result<Order> saveOrUpdate(Order order) {
        if(ZwzNullUtils.isNull(order.getCustomerId())) {
            return ResultUtil.error("订单客户不能为空");
        }
        Customer customer = iCustomerService.getById(order.getCustomerId());
        if(customer == null) {
            return ResultUtil.error("订单客户已被删除");
        }
        order.setCustomerName(customer.getName());
        if(iOrderService.saveOrUpdate(order)){
            return new ResultUtil<Order>().setData(order);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增订单信息")
    public Result<Order> insert(Order order) {
        if(ZwzNullUtils.isNull(order.getCustomerId())) {
            return ResultUtil.error("订单客户不能为空");
        }
        Customer customer = iCustomerService.getById(order.getCustomerId());
        if(customer == null) {
            return ResultUtil.error("订单客户已被删除");
        }
        User currUser = securityUtil.getCurrUser();
        order.setUserName(currUser.getNickname());
        order.setCustomerName(customer.getName());
        order.setDate(DateUtil.today());
        order.setCode(IdUtil.getSnowflake(1, 1).nextId() + "");
        iOrderService.saveOrUpdate(order);
        return new ResultUtil<Order>().setData(order);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑订单信息")
    public Result<Order> update(Order order) {
        if(ZwzNullUtils.isNull(order.getCustomerId())) {
            return ResultUtil.error("订单客户不能为空");
        }
        Customer customer = iCustomerService.getById(order.getCustomerId());
        if(customer == null) {
            return ResultUtil.error("订单客户已被删除");
        }
        order.setCustomerName(customer.getName());
        iOrderService.saveOrUpdate(order);
        return new ResultUtil<Order>().setData(order);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除订单信息")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iOrderService.removeById(id);
        }
        return ResultUtil.success();
    }
}
